package main;


import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;


public class MainReader {
    private HashMap<String, HashMap<String, Integer>> word;
    ArrayList<String[]> wordTokens;
    ArrayList<Integer> sentences;
    public MainReader(String filename){
	String [] str;
	ArrayList<String[]> arr = new ArrayList<String[]>();
	sentences = new ArrayList<Integer>();
	try{
	    FileInputStream fis = new FileInputStream(filename);
	    DataInputStream ds = new DataInputStream(fis);
	    BufferedReader br = new BufferedReader(new InputStreamReader(ds));

	    String line = "";
	    while((line = br.readLine()) != null){
		
		if(line.equals("")){
		    sentences.add(arr.size()); //TODO: may need to subtract 1    
		}
		
		String[] raw = line.split("\\t");
		if(raw.length == 6 && isAlpha(raw[4])){	// Filter
		    str = new String[3]; //0 is word, 1 is pos, 2 is ppos
		    str[0] = raw[2];
		    str[1] = raw[4];
		    str[2] = raw[5];
		    //System.out.println(str[0] +  " " + str[1] +" "+ str[2]);
		    arr.add(str);
		}
		
	    }
	}catch(IOException e){
	}
	this.wordTokens = arr;
    }
    
    
    private  boolean isAlpha(String name) {
	    return name.matches("[a-zA-Z]+");
}

    public HashMap<String, HashMap<String, Integer>> read(){
	word = new HashMap<String, HashMap<String,Integer>>();
	for( String [] str : wordTokens){
	    if(word.containsKey(str[0])){ //word
		//TODO contains type key
		int val = 0;
		if(word.get(str[0]).containsKey(str[1]))
		    val = word.get(str[0]).get(str[1]);
		else
		    word.get(str[0]).put(str[1], 1);
		word.get(str[0]).put(str[1], ++val);
	    }else{
		final String tmp = str[1];
		word.put(str[0], new HashMap<String, Integer>(){{put(tmp ,1);}});
	    }
	}
	return word;
    }	
    
    public ArrayList<Integer> getSentences(){
    	return sentences;
    }
    public ArrayList<String[]> getWordTokens(){
	return wordTokens;
    }

}
